#!/bin/sh

prefix="@prefix@"
exec_prefix="@exec_prefix@"
exec_prefix_set=no
datarootdir="@datarootdir@"

release="@release@"
debug="@debug@"
gprof="@gprof@"
cprof="@cprof@"
optim="@optim@"

plugins="@PLUGINS@ "
builtins="@BUILTINS@ "

cppflags=""
cflags=""
cxxflags=""
objcflags=""
ldflags=""
libs=""

cflags_tuning="@CFLAGS_TUNING@"
cflags_optim_size="@CFLAGS_OPTIM_SIZE@"
cflags_optim_speed="@CFLAGS_OPTIM_SPEED@"
cflags_optim_nodebug="@CFLAGS_OPTIM_NODEBUG@"
cflags_nooptim="@CFLAGS_NOOPTIM@"

#
#  Do not touch below this place unless you really know what you are doing
#
usage()
{
        cat << BLAH
Usage: vlc-config OPTIONS MODULES
Options:
        [--prefix[=DIR]]          set prefix
        [--exec-prefix[=DIR]]     set exec prefix
        [--version]               print version and exit
        [--linkage]               print linkage mode (c, c++, objc)
        [--list]                  print modules names and exit
        [--libs]                  output linking flags
        [--cflags]                output C compilation flags
        [--cxxflags]              output C++ compilation flags
        [--objcflags]             output Objective C compilation flags
Modules:
        vlc                       the main VLC object
        plugin                    flags for plugin modules
        builtin                   flags for built-in modules
        pic                       flags for PIC code
        MODULE                    any available module (dummy, gtk, avi, etc.)
BLAH
        exit $1
}

register_flags()
{
  case "$1" in
    #@1@#
    *)
      ;;
  esac
}

if test $# -eq 0; then
        usage 1 1>&2
fi

#
#  No need to include the default @*FLAGS@ values here because they are
#  automatically added when using $(COMPILE), $(CXXCOMPILE) or $(OBJCCOMPILE)
#
if test "@includedir@" != "/usr/include"; then
  includes="-I@includedir@"
fi
if test "${top_builddir}" != ""; then
  top_builddir="${top_builddir}/"
elif test "${TOP_BUILDDIR}" != ""; then
  top_builddir="${TOP_BUILDDIR}/"
fi
includes="${includes}"
cppflags="${includes}"
module=""
linkage="c"

#
#  On Linux and Solaris, activate 64-bit off_t (by default under BSD)
#
cppflags="${cppflags} -D_FILE_OFFSET_BITS=64 -D__USE_UNIX98 -D_LARGEFILE64_SOURCE -D_REENTRANT -D_THREAD_SAFE"

#
#  Various additional defines
#
if [ "${debug}" = yes ]; then
  cppflags="${cppflags} -DDEBUG"
  cflags="${cflags} -g"
  cxxflags="${cxxflags} -g"
  objcflags="${objcflags} -g"
  ldflags="${ldflags} -g"
fi
if [ "${cprof}" = yes ]; then
  cppflags="${cppflags} -DCPROF"
  cflags="${cflags} -finstrument-functions"
  cxxflags="${cxxflags} -finstrument-functions"
  objcflags="${objcflags} -finstrument-functions"
fi
if [ "${gprof}" = yes ]; then
  cppflags="${cppflags} -DGPROF"
  cflags="${cflags} -pg"
  cxxflags="${cxxflags} -pg"
  objcflags="${objcflags} -pg"
  ldflags="${ldflags} -pg"
fi
if [ "${release}" = yes ]; then
  cppflags="${cppflags} -DHAVE_RELEASE"
fi
if [ "${optim}" = size ]; then
  cflags="${cflags} ${cflags_optim_size} ${cflags_tuning}"
  cxxflags="${cxxflags} ${cflags_optim_size} ${cflags_tuning}"
  objcflags="${objcflags} ${cflags_optim_size} ${cflags_tuning}"
  if [ "${debug}" != yes -a "${gprof}" != yes -a "${cprof}" != yes ]; then
    cflags="${cflags} ${cflags_optim_nodebug}"
    cxxflags="${cxxflags} ${cflags_optim_nodebug}"
    objcflags="${objcflags} ${cflags_optim_nodebug}"
  fi
elif [ "${optim}" = speed ]; then
  cflags="${cflags} ${cflags_optim_speed} ${cflags_tuning}"
  cxxflags="${cxxflags} ${cflags_optim_speed} ${cflags_tuning}"
  objcflags="${objcflags} ${cflags_optim_speed} ${cflags_tuning}"
  if [ "${debug}" != yes -a "${gprof}" != yes -a "${cprof}" != yes ]; then
    cflags="${cflags} ${cflags_optim_nodebug}"
    cxxflags="${cxxflags} ${cflags_optim_nodebug}"
    objcflags="${objcflags} ${cflags_optim_nodebug}"
  fi
else
  cflags="${cflags} ${cflags_nooptim}"
  cxxflags="${cxxflags} ${cflags_nooptim}"
  objcflags="${objcflags} ${cflags_nooptim}"
fi

#
#  The main argument loop
#
while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/-[_a-zA-Z0-9\-]*=//'` ;;
  *) optarg= ;;
  esac

  # Mangle plugin name, if applicable
  # This is just a convenience hack for modules/common.am
  tgt="$1"
  tgt="${tgt##*/}"
  case "$tgt" in
    lib*_plugin_la-*.lo)
      tgt="${tgt#*lib}"
      tgt="${tgt%_plugin_la-*.lo}"
      ;;
    lib*_plugin.la)
      tgt="${tgt#lib}"
      tgt="${tgt%_plugin.la}"
      ;;
    *)
      ;;
  esac

  case "$tgt" in
    --prefix=*)
      prefix="${optarg}"
      if test "${exec_prefix_set}" = no ; then
        exec_prefix="${optarg}"
      fi
      ;;
    --prefix)
      echo_prefix=yes
      ;;
    --exec-prefix=*)
      exec_prefix="${optarg}"
      exec_prefix_set=yes
      ;;
    --exec-prefix)
      echo_exec_prefix=yes
      ;;
    --version)
      echo "@VERSION@"
      exit 0
      ;;
    --linkage)
      echo_linkage=yes
      ;;
    --list)
      echo_list=yes
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --cppflags)
      echo_cppflags=yes
      ;;
    --cxxflags)
      echo_cxxflags=yes
      ;;
    --objcflags)
      echo_objcflags=yes
      ;;
    --ldflags)
      echo_ldflags=yes
      ;;
    --libs|-libs)
      echo_libs=yes
      ;;
    -*)
      usage 1 1>&1
      ;;
    libvlccore)
      cppflags="${cppflags} -D__LIBVLC__ -I${top_builddir}src/misc"
      ;;
    plugin)
      echo_plugin=yes
      cppflags="${cppflags} -D__LIBVLC__ -D__PLUGIN__"
      ;;
    builtin)
      echo_builtin=yes
      cppflags="${cppflags} -D__LIBVLC__ -D__BUILTIN__"
      ;;
    pic)
      ;;
    mozilla)
      ;;
    *)
      module="$tgt"
      ;;
  esac

  # Register per-module *FLAGS
  register_flags "$tgt"

  shift
done

#
#  If a module was requested, use its name
#
if test -n "${module}"; then
  cppflags="${cppflags} -DMODULE_NAME=${module} -DMODULE_NAME_IS_${module} -DMODULE_STRING=\"${module}\""
fi

#
#  Output what we were asked
#
if test "${echo_linkage}" = yes; then
  echo "${linkage}"
  exit 0
fi

if test "${echo_list}" = yes; then
  if test "${echo_plugin}" = yes; then
    echo "${plugins}"
    printf '\n'
  fi
  if test "${echo_builtin}" = yes; then
    echo "${builtins}"
    printf '\n'
  fi
  exit 0
fi

if test "${echo_prefix}" = yes; then
  echo "${prefix}"
fi
if test "${echo_exec_prefix}" = yes; then
  echo "${exec_prefix}"
fi
if test "${echo_cppflags}" = yes; then
  echo "${cppflags}"
fi
if test "${echo_cflags}" = yes; then
  echo "${cppflags} ${cflags}"
fi
if test "${echo_cxxflags}" = yes; then
  echo "${cppflags} ${cxxflags}"
fi
if test "${echo_objcflags}" = yes; then
  echo "${cppflags} ${objcflags}"
fi
if test "${echo_ldflags}" = yes; then
  echo "${ldflags}"
fi

# Libs
# There are 4 possibilities
#  - We are a plugin or a builtin
#  - We are building something from the inside (builtin)
#       - Link with builtins in place
#  If you want something shared from the inside (binding),
#  you need "builtin vlc"
if test "${echo_libs}" = yes; then
  echo "${libs}"
fi
